<?php 

class GoodsModel extends Model{
	public $table = "goods";
	public $validate = array(
		array('pname','nonull','商品名不能为空',2,3),
		array('gunit','nonull','单位不能为空',2,3),
		array('marketprice','nonull','市场价不能为空',2,3),
		array('shopprice','nonull','商城价不能为空',2,3),
		array('listview','nonull','列表图必须上传',2,3),
		array('click','nonull','点击数不能为空',2,3),
	);
	public function edit($gid){
	    if(!$this->create()) return FALSE;
		if(!$this->create()) return FALSE;
		$cid = Q('post.cid',0,"intval");
		$typeData = K("category")->where("cid={$cid}")->find();
//			组合商品信息数据
		$goodsData = array(
			"gname" => Q('post.pname'),
//				"gno" => Q('post.'),//货号
			"gunit" => Q('post.gunit'),
			"marketprice" => Q('post.marketprice'),
			"shopprice" => Q('post.shopprice'),
//				"stock" => Q('post.'),//库存
			"listview" => Q('post.listview'),
			"click" => Q('post.click'),
			"time" => time(),
			"cid" => $cid,
			"tid" => $typeData["typeid"],
			"aid" => $_SESSION["aid"],
		);
//			修改商品信息到商品表
		$this->where("gid={$gid}")->update($goodsData);
		$bigpic = '';
		$middlepic = '';
		$samllpic = '';
		//商品图册的缩略图处理
		if(isset($_POST['goodspic'])){
			$img = new Image;
			$bigpic = array();
			$middlepic = array();
			$samllpic = array();
			foreach (Q('post.goodspic',array()) as $k => $v) {
				$bigpic[] = $v;
				$middlepic[] = $img->thumb($v,"",500,500,5);
				$samllpic[] = $img->thumb($middlepic[$k],"",200,200,5);
			}
			$bigpic = implode(",", $bigpic);
			$middlepic = implode(",", $middlepic);
			$samllpic = implode(",", $samllpic);
		}
//			组合商品详细表信息
		$minuteData = array(
			"samllpic" => $samllpic,
			"middlepic" => $middlepic,
			"bigpic" => $bigpic,
			"commoditydetails" =>$_POST['content'],
			"service" => $_POST['server'],
			"gid" => $gid,
		);
//			修改商品详细
		$commoditydetailsmodel = K("Commoditydetails");
		if(!$commoditydetailsmodel->where("gid={$gid}")->update($minuteData)){
			$this->error = $commoditydetailsmodel->error;
			return FALSE;
		}
		$specsModel = K("Specs");
		$specsModel->where("gid={$gid}")->delete();
//			组合商品属性表数据
		if(isset($_POST['attrval'])){
			$attrData = array();
			$attrval = $_POST['attrval'];
			foreach ($attrval as $k => $v) {
				if(!is_array($v)){
					$attrData[] = array(
						"attrval" => $v,
						"fuelprices" => 0,
						"gid" => $gid,
						"typeattrid" => $k,
					);
				}else{
					foreach ($v['val'] as $key => $value) {
						$attrData[] = array(
							"attrval" => $value,
							"fuelprices" => $v['fuelprices'][$key],
							"gid" => $gid,
							"typeattrid" => $k,
						);
					}
				}
			}
			foreach ($attrData as $value) {
				if(!$specsModel->add($value)){
					$this->error = $specsModel->error;
					return FALSE;
				}
			}
		}
		return TRUE;
	}
	public function addData(){
    	if(!$this->create()) return FALSE;
		$cid = Q('post.cid',0,"intval");
		$typeData = K("category")->where("cid={$cid}")->find();
//			组合商品信息数据
		$goodsData = array(
			"gname" => Q('post.pname'),
//				"gno" => Q('post.'),//货号
			"gunit" => Q('post.gunit'),
			"marketprice" => Q('post.marketprice'),
			"shopprice" => Q('post.shopprice'),
//				"stock" => Q('post.'),//库存
			"listview" => Q('post.listview'),
			"click" => Q('post.click'),
			"time" => time(),
			"cid" => $cid,
			"tid" => $typeData["typeid"],
			"aid" => $_SESSION["aid"],
		);
//			添加商品信息到商品表，返回商品表中的pid
		$pid = $this->add($goodsData);
		$bigpic = '';
		$middlepic = '';
		$samllpic = '';
		//商品图册的缩略图处理
		if(isset($_POST['goodspic'])){
			$img = new Image;
			$bigpic = array();
			$middlepic = array();
			$samllpic = array();
			foreach (Q('post.goodspic',array()) as $k => $v) {
				$bigpic[] = $v;
				$middlepic[] = $img->thumb($v,"",500,500,5);
				$samllpic[] = $img->thumb($middlepic[$k],"",200,200,5);
			}
			$bigpic = implode(",", $bigpic);
			$middlepic = implode(",", $middlepic);
			$samllpic = implode(",", $samllpic);
		}
//			组合商品详细表信息
		$minuteData = array(
			"samllpic" => $samllpic,
			"middlepic" => $middlepic,
			"bigpic" => $bigpic,
			"commoditydetails" =>$_POST['content'],
			"service" => $_POST['server'],
			"gid" => $pid,
		);
//			添加商品详细到商品详细表,返回详细表id  21232f297a57a5a743894a0e4a801fc3
		$commoditydetailsmodel = K("Commoditydetails");
		if(!$mid = $commoditydetailsmodel->add($minuteData)){
			$this->error = $commoditydetailsmodel->error;
			return FALSE;
		}
//			组合商品属性表数据
		if(isset($_POST['attrval'])){
			$attrData = array();
			$attrval = $_POST['attrval'];
			foreach ($attrval as $k => $v) {
				if(!is_array($v)){
					$attrData[] = array(
						"attrval" => $v,
						"fuelprices" => 0,
						"gid" => $pid,
						"typeattrid" => $k,
					);
				}else{
					foreach ($v['val'] as $key => $value) {
						$attrData[] = array(
							"attrval" => $value,
							"fuelprices" => $v['fuelprices'][$key],
							"gid" => $pid,
							"typeattrid" => $k,
						);
					}
				}
			}
			$specsModel = K("Specs");
			foreach ($attrData as $value) {
				if(!$specsModel->add($value)){
					$this->error = $specsModel->error;
					return FALSE;
				}
			}
		}
		return TRUE;
	}
}
